/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.util;

import java.io.BufferedReader;
import java.io.Reader;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Path;
import org.eclipse.ui.IEditorPart;
import test.org.zoundry.gaedt.core.ZGaeCoreTestCase;

/**
 * Tests the resourceUtil util class.
 */
public class ZGaeResourceUtilTest extends ZGaeCoreTestCase
{
    public void testGetContents() throws Exception
    {
        String content = "The quick brown fox jumped over the lazy dog.";

        IProject project = getOrCreateTestProject(this.getClass().getSimpleName());
        IFile file = createTestFile(project, new Path("testGetContents.txt"), content);

        String expected = content;
        String actual = ZGaeResourceUtil.getContents(file).trim();
        assertEquals(expected, actual);
    }

    public void testOpenEditor() throws Exception
    {
        String content = "The quick brown fox jumped over the lazy dog.";

        IProject project = getOrCreateTestProject(this.getClass().getSimpleName());
        IFile file = createTestFile(project, new Path("testOpenEditor.txt"), content);

        IEditorPart editor = ZGaeResourceUtil.openEditor(file);
        assertNotNull("The editor was null but should not have been.", editor);
        assertEquals("org.eclipse.ui.editors.text.TextEditor", editor.getClass().getName());
    }

    public void testGetReader() throws Exception
    {
        String content = "The quick brown fox jumped over the lazy dog.";

        IProject project = getOrCreateTestProject(this.getClass().getSimpleName());
        IFile file = createTestFile(project, new Path("testGetReader.txt"), content);

        Reader reader = ZGaeResourceUtil.getReader(file);
        assertNotNull("The reader was null but should not have been.", reader);
        BufferedReader breader = new BufferedReader(reader);
        String expected = content;
        String actual = breader.readLine().trim();
        assertEquals(expected, actual);
    }

}
